package com.leetcode.partition10;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author `RKC`
 * @date 2022/2/19 21:47
 */
public class LC969煎饼排序 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        int[] nums = new int[ss.length];
        for (int i = 0; i < ss.length; i++) nums[i] = Integer.parseInt(ss[i]);
        writer.write(pancakeSort(nums) + "\n");
        writer.flush();
    }

    public static List<Integer> pancakeSort(int[] arr) {
        int n = arr.length;
        List<Integer> answer = new ArrayList<>();
        //每次从可选区间中选择当前的最大值，然后把这次的最大翻到最前面，再把这次最大翻到对应位置
        for (int i = 0; i < n; i++) {
            int idx = -1;
            for (int j = 0; j < n - i; j++) {
                if (idx == -1 || (arr[idx] < arr[j])) idx = j;
            }
            //如果当前最大就是在当前最末，就不需要动
            if (idx == n - i - 1) continue;
            reverse(arr, 0, idx);
            answer.add(idx + 1);
            reverse(arr, 0, n - i - 1);
            answer.add(n - i);
        }
        return answer;
    }

    private static void reverse(int[] arr, int l, int r) {
        while (l < r) {
            int t = arr[l];
            arr[l++] = arr[r];
            arr[r--] = t;
        }
    }
}
